package com.bjpowernode.front.controller;

import com.bjpowernode.api.model.ProductInfo;
import com.bjpowernode.api.pojo.BidInfoProduct;
import com.bjpowernode.api.pojo.MultiProduct;
import com.bjpowernode.common.enums.RCode;
import com.bjpowernode.common.util.CommonUtil;
import com.bjpowernode.front.view.PageInfo;
import com.bjpowernode.front.view.RespResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(tags = "产品信息功能")
@RequestMapping("/v1")
public class ProductController extends BaseController {

    @GetMapping("/product/index")
    @ApiOperation(value = "首页三类产品信息", httpMethod = "GET", notes = "首页产品信息，一共查询三个产品")
    public RespResult queryProductIndex() {
        MultiProduct multiProduct = productService.queryIndexPageProducts();
        return RespResult.ok(multiProduct);
    }
    /*某个产品类型分页查询*/
    @ApiOperation(value = "某个产品类型分页查询", httpMethod = "GET", notes = "某个产品类型分页查询")
    @GetMapping("/product/list")
    public RespResult queryProductByType(@RequestParam Integer ptype,
                                         @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                         @RequestParam(value = "pageSize", required = false, defaultValue = "9") Integer pageSize) {
        RespResult result = new RespResult();
        if (ptype != null && (ptype == 0 || ptype == 1 || ptype == 2)) {
            pageNo = CommonUtil.defaultPageNo(pageNo);
            pageSize = CommonUtil.defaultPageSize(pageSize);
            //分页处理，记录总数
            Integer recordNums = productService.queryRecordNumsByType(ptype);
            if (recordNums > 0) {
                //产品集合
                List<ProductInfo> productInfos = productService.queryByTypeLimit(ptype, pageNo, pageSize);
                //构建PageInfo
                PageInfo pageInfo = new PageInfo(pageNo, pageSize, recordNums);
                result= RespResult.ok();
                result.setList(productInfos);
                result.setPage(pageInfo);
            } else {
                //请求参数有误
                result.setRCode(RCode.REQUEST_PRODUCT_TYPE_ERR);
            }
        }
        return result;
    }


    /*根据产品id查询产品信息和投资记录*/
    @ApiOperation(value = "根据产品id查询产品信息和投资记录", httpMethod = "GET", notes = "根据产品id查询产品信息和投资记录")
    @GetMapping("/product/info")
    public RespResult queryProductDetail(@RequestParam("productId") Integer id){
        if (id != null && id > 0){
            ProductInfo productInfo = productService.queryByProductId(id);
            if (productInfo != null){
                List<BidInfoProduct> bidInfoProducts = investService.queryBidListByProductId(id, 1, 5);
                return RespResult.ok(productInfo,bidInfoProducts);
            }
            return RespResult.fail(RCode.PRODUCT_NOT_FOUND);
        }
        return RespResult.fail();
    }
}
